package com.shcowin.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shcowin.biz.NewsBiz;
import com.shcowin.entity.News;

/**
 * @author weiweiqin
 * @createDate 2013-3-1 ����10:18:02
 * @file HomeController.java
 * @package com.shcowin.controller
 * @project shcowin
 * @blog http://rather-lonely.iteye.com/
 * @email 13773607653@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/news")
public class NewsController {

	@Inject
	private NewsBiz<News> newsBiz;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(News news) {
		newsBiz.add(news);
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(News news) {
		newsBiz.edit(news);
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getById(Integer id) {
		News news = newsBiz.getById(id);
		if (null == news) {
			return null;
		}
		Map<String, Object> json = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		json.put("news", news);
		json.put("date", sdf.format(news.getDate()));
		return json;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Map<String, Object> del(int id) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			newsBiz.del(id);
		} catch (Exception e) {
			json.put("result", false);
			return json;
		}
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list() {
		List<News> newsList = newsBiz.getAll();
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != newsList && !newsList.isEmpty()) {
			json.put("news", newsList);
			for (News news : newsList) {
				datas.add(sdf.format(news.getDate()));
			}
			json.put("datas", datas);
		}
		return json;
	}

	@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getByPage(int currPage) {
		int start = (currPage - 1) * 10;
		List<News> newsList = newsBiz.getByPage(start, 10);
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != newsList && !newsList.isEmpty()) {
			json.put("news", newsList);
			for (News news : newsList) {
				datas.add(sdf.format(news.getDate()));
			}
			json.put("datas", datas);
			json.put("totalSize", Math.ceil(newsBiz.getCount() / 10.0));
		}
		return json;
	}

	@RequestMapping(value = "/getDynamicByPage", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDynamicByPage(int currPage) {
		int start = (currPage - 1) * 10;
		List<News> newsList = newsBiz.getDynamicByPage(start, 10);
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != newsList && !newsList.isEmpty()) {
			json.put("news", newsList);
			for (News news : newsList) {
				datas.add(sdf.format(news.getDate()));
			}
			json.put("datas", datas);
			json.put("totalSize", Math.ceil(newsBiz.getCount() / 10.0));
		}
		return json;
	}

	@RequestMapping(value = "/getNewsByPage", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getNewsByPage(int currPage) {
		int start = (currPage - 1) * 10;
		List<News> newsList = newsBiz.getNewsByPage(start, 10);
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != newsList && !newsList.isEmpty()) {
			json.put("news", newsList);
			for (News news : newsList) {
				datas.add(sdf.format(news.getDate()));
			}
			json.put("datas", datas);
			json.put("totalSize", Math.ceil(newsBiz.getCount() / 10.0));
		}
		return json;
	}
}
